import json
from configparser import ConfigParser

from utils.network_utils import NetworkUtils


class ProxyManager:
    def __init__(self):
        self.proxies = self._load_proxies()

    def _load_proxies(self):
        """从配置文件加载代理"""
        config = ConfigParser()
        config.read('configs/proxies.json')
        return json.loads(config.get('proxies', 'list', fallback='[]'))

    def get_available_proxy(self):
        """获取可用代理"""
        for proxy in self.proxies:
            if NetworkUtils.validate_proxy(proxy):
                return proxy
        return None